package com.mjlf.mr.speed;

import com.mjlf.mr.utils.DateFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.text.ParseException;
import java.util.Date;

/**
 * @ClassName: SpeedGroupComparator
 * @Author: majun
 * @CreateDate: 2018/12/18 11:46
 * @Version: 1.0
 * @Description: TODO
 */

public class SpeedGroupComparator extends WritableComparator {

    public SpeedGroupComparator() {
        super(Speed.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        int result = 0;

        Speed s1 = (Speed)a;
        Speed s2 = (Speed)b;

        try {
            Date d1 = DateFormat.parse(s1.getTime(), "yyyy-MM-dd");
            Date d2 = DateFormat.parse(s2.getTime(), "yyyy-MM-dd");

            if(d1.equals(d2)){
                result = 0;
            }else {
                if(d1.before(d2)){
                    result = 1;
                }else {
                    result = -1;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  result;
    }
}
